﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Globalization;
using System.Linq;

using ProjectBase.Core;

using $prjsn$.Common;
using $prjsn$.Common.Business.Services;
using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class LangFacade : BaseFacade<Lang, int, LangFilter, ILangDao>, ILangFacade
    {
        #region IBaseFacade<T> override - FACTORY METHOD
        /// <summary>
        ///     Factory Method pattern.
        ///     Returns new transient entity 'Lang'.
        ///     If overriden, some properties could be preset to business defaults.
        ///     (Business rules therefore should be met)
        /// </summary>
        /// <returns>new entity 'Lang' transient</returns>
        public override Lang CreateNew()
        {
            var entity = new Lang();
            // TODO preset entity
            return entity;
        }
        #endregion IBaseFacade<T> override - FACTORY METHOD

        #region constructor
        static LangFacade()
        {
            ChangeHandler += Clear;
        }
        #endregion constructor

        #region ILangFacade Members
        /// <summary>
        ///     Returns all languages in the system
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<Lang> GetAll()
        {
            var languages = Langs;
            if (languages.IsNull())
            {
                languages = Dao.GetAll().Select(e => e.Clone() as Lang).ToList().AsReadOnly();
                if (languages.IsEmpty())
                {
                    languages = new List<Lang> { new Lang { LanguageName = "en" } };
                }
                Langs = languages;
            }
            return languages;
        }
        #endregion

        #region Static list
        protected static IEnumerable<Lang> Langs { get; set; }

        static void Clear(object sender, EventArgs e)
        {
            Langs = null;
        }
        #endregion Static list

        #region Validation
        public virtual bool IsLanguageNameValid(string languageName)
        {
            if (CultureInfo.GetCultures(CultureTypes.AllCultures)
                .ToList()
                .Any(c => c.Name.Equals(languageName, StringComparison.Ordinal)))
            {
                return true;
            }
            this.PublishError(Str.Messages.LanguageNotExists1
                , languageName);
            return false;
        }
        #endregion Validation
    }
}